/**
 * 
 */
package com.et114.test.compass;

import java.util.ArrayList;
import java.util.List;

import org.compass.core.CompassCallback;
import org.compass.core.CompassException;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;

//import com.et114.test.compass.model.Test;
import com.et114.modules.org.vo.UserVo;

/**
 * @author guanhw
 */
public class UserListResultCallback implements CompassCallback {
	private String key;
	private int startIndex;
	private int maxResult;

	/**
	 * 
	 */
	public UserListResultCallback ( String key , int startIndex , int maxResult ) {
		// TODO Auto-generated constructor stub
		this.key = key ; 
		this.startIndex = startIndex ; 
		this.maxResult = maxResult ; 
	}
	
	/* (non-Javadoc)
	 * @see org.compass.core.CompassCallback#doInCompass(org.compass.core.CompassSession)
	 */
	public Object doInCompass ( CompassSession session ) throws CompassException {
		// TODO Auto-generated method stub
		List datas = new ArrayList ( );
		CompassHits hits = session.queryBuilder ( ).queryString ( key )
				.toQuery ( ).hits ( );
		
		int lastIndex = startIndex + maxResult - 1;
		if ( lastIndex > ( hits.length ( ) - 1 ) )
			lastIndex = hits.length ( ) - 1;
		
		System.out.println ( "lastIndex ....." + lastIndex ) ; 
		for ( int i = startIndex ; i <= lastIndex ; i ++ ) {
			UserVo add = ( UserVo ) hits.data ( i );
			// 设置高亮显示
			if ( hits.highlighter ( i ).fragment ( "loginName" ) != null ) {
				add.setLoginName (  hits.highlighter ( i ).fragment ( "loginName" ) );
			}
		
			datas.add ( add );
		}
		return datas;
	}

	public String getKey ( ) {
		return key;
	}

	public void setKey ( String key ) {
		this.key = key;
	}

	public int getStartIndex ( ) {
		return startIndex;
	}

	public void setStartIndex ( int startIndex ) {
		this.startIndex = startIndex;
	}

	public int getMaxResult ( ) {
		return maxResult;
	}

	public void setMaxResult ( int maxResult ) {
		this.maxResult = maxResult;
	}
	
}
